import React from 'react';
import {injectIntl} from './intl';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import TextInput from 'Shared/components/TextInput';
import DateRangePicker from 'Shared/components/DateRangePicker';
import {FORM_OPTIONS} from '../constants';
import WrappedSelect from '../common/WrappedSelect';
import {dealerType} from '../Enum';
const FormItem = Form.Item;
const EMPTY_ARRAY = [];
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';

const dealerConditions = {
    type: dealerType.总代
};

class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }

    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }

    render() {
        const dealerInfo = this.props.conditions.dealerId ? {
            id: this.props.conditions.dealerId,
            code: this.props.conditions.dealerCode,
            name: this.props.conditions.dealerName
        } : null;
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        {this.props.brands.length > 1 && <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.brandId',
                                    defaultMessage: '品牌'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    name="brandId"
                                    allowClear
                                    value={this.props.conditions.brandId}
                                    options={this.props.brands}
                                    onChange={this.props.onConditionsChange} />
                            </FormItem>
                        </Col>}
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.dealerId',
                                    defaultMessage: '总代'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DealerSelectPanel
                                    conditions={dealerConditions}
                                    value={dealerInfo}
                                    onSelect={this.props.onSelect} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.partCode',
                                defaultMessage: '零件号'
                            })}{...FORM_OPTIONS.item}>
                                <TextInput
                                    name="partCode"
                                    value={this.props.conditions.partCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.productCategoryCode',
                                defaultMessage: '车型'
                            })}{...FORM_OPTIONS.item}>
                                <TextInput
                                    name="productCategoryCode"
                                    value={this.props.conditions.productCategoryCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.lv1FaultItemCode',
                                defaultMessage: '故障模式一级'
                            })}{...FORM_OPTIONS.item}>
                                <TextInput
                                    name="lv1FaultItemCode"
                                    value={this.props.conditions.lv1FaultItemCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.lv2FaultItemCode',
                                defaultMessage: '故障模式二级'
                            })}{...FORM_OPTIONS.item}>
                                <TextInput
                                    name="lv2FaultItemCode"
                                    value={this.props.conditions.lv2FaultItemCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.lv3FaultItemCode',
                                defaultMessage: '故障模式三级'
                            })}{...FORM_OPTIONS.item}>
                                <TextInput
                                    name="lv3FaultItemCode"
                                    value={this.props.conditions.lv3FaultItemCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.insertDate',
                                    defaultMessage: '结转日期'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DateRangePicker name="insertDate"
                                    value={this.props.conditions.insertDate}
                                    onChange={this.props.onConditionsChange} />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="operation-buttons">
                        <Col span={6}>
                            <Button
                                key="query"
                                type="primary"
                                onClick={this.props.querySearch}
                                loading={this.props.loading}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}
                            </Button>
                            <Button key="clear" onClick={this.props.onClickReset}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}
                            </Button>
                        </Col>
                    </Row>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    brands: PropTypes.array.isRequired,
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickReset: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onConditionsPressEnter: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    intl: PropTypes.object,
};
import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel, selectDealerQuery} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);

const getBrandsData = createSelector(
    state => state.getIn(['page', 'domainData', 'initData', 'brands']),
    initData => {
        const data = initData.toJS() || EMPTY_ARRAY;
        return data.map(item => ({
            text: item.name,
            value: item.id
        }));
    }
);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    permissions: getPermissions(state),
    brands: getBrandsData(state)
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (value, name) => dispatch(saveQueryCondition(name, value)),
    onConditionsPressEnter: (value, name) => {
        dispatch(saveQueryCondition(name, value));
        dispatch(querySearch());
    },
    querySearch: () => dispatch(querySearch()),
    onClickReset: () => dispatch(resetQueryPanel()),
    onSelect: data => dispatch(selectDealerQuery(data))
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(QueryPanel));
